package dcc.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import dcc.component.SearchResultTable;
import dcc.mbg.model.BaseInfo;
import dcc.service.CommonService;
import dcc.tools.BeanFactory;

public class SearchWindow extends JFrame {

	private JPanel contentPane;
	private SearchResultTable table;
	private JTextField tfSearchKey;
	private JButton btnSearch;

	public SearchWindow() {
		setTitle("\u4EBA\u624D\u67E5\u8BE2");
		this.addWindowFocusListener(new WindowFocusListener() {

			@Override
			public void windowGainedFocus(WindowEvent e) {
				// TODO Auto-generated method stub
				Search();
			}

			@Override
			public void windowLostFocus(WindowEvent e) {
				// TODO Auto-generated method stub
				// Nothing
			}

		});
		setResizable(false);
		setName("");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 685, 515);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 42));
		contentPane.add(panel, BorderLayout.NORTH);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 195, 395, 0, 0 };
		gbl_panel.rowHeights = new int[] { 32, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblNewLabel = new JLabel(
				"\u8F93\u5165\u4EBA\u624D\u8BC6\u522B\u53F7/\u4EBA\u624D\u59D3\u540D:");
		lblNewLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.SOUTH;
		gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		panel.add(lblNewLabel, gbc_lblNewLabel);

		tfSearchKey = new JTextField();
		tfSearchKey.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15));
		GridBagConstraints gbc_tfSearchKey = new GridBagConstraints();
		gbc_tfSearchKey.insets = new Insets(0, 0, 0, 5);
		gbc_tfSearchKey.anchor = GridBagConstraints.SOUTH;
		gbc_tfSearchKey.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfSearchKey.gridx = 1;
		gbc_tfSearchKey.gridy = 0;
		panel.add(tfSearchKey, gbc_tfSearchKey);
		tfSearchKey.setColumns(10);

		btnSearch = new JButton("\u67E5\u8BE2");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Query DB
				Search();
			}
		});
		btnSearch.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
		GridBagConstraints gbc_btnSearch = new GridBagConstraints();
		gbc_btnSearch.anchor = GridBagConstraints.SOUTH;
		gbc_btnSearch.gridx = 2;
		gbc_btnSearch.gridy = 0;
		panel.add(btnSearch, gbc_btnSearch);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		table = new SearchResultTable(this);
		scrollPane.setViewportView(table);
	}

	private void Search() {
		if ("".equals(tfSearchKey.getText())) {
			// TODO search all records
			List<BaseInfo> records = ((CommonService) BeanFactory
					.getBean("commonService")).getAllRecord();
			// TODO show in table
			table.updateData(records);
		} else {
			// TODO search all records
			List<BaseInfo> records = ((CommonService) BeanFactory
					.getBean("commonService"))
					.getRecordsByShiBieHaoOrXingMing(tfSearchKey.getText().trim());
			// TODO show in table
			table.updateData(records);
		}
	}

}
